<template>
  <div class="container">
    <div class="nav">
      <!-- 导航 -->
      <xwBreadCrumb />
      <el-link type="primary" href="/admin/nowplaying">返回列表页</el-link>
    </div>
    <div class="form">
      <el-form :model="form" ref="form" label-width="100px">
        <el-tabs v-model="activeName">
          <el-tab-pane label="电影基础信息" name="0">
            <el-form-item label="电影标题">
              <el-input v-model="form.title"></el-input>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="电影封图信息" name="1">电影封图信息</el-tab-pane>
          <el-tab-pane label="电影预告片" name="2">
            <el-upload :action="`${host}/api/upvideo`" :show-file-list="false" :on-success="uploadSuccess">
              <el-button type="primary">上传视频</el-button>
            </el-upload>
            <br />
            <video v-if="video != ''" :src="video" controls poster="http://img.1314000.cn/swiper-1.jpeg" />
          </el-tab-pane>
          <el-tab-pane label="电影详情信息" name="3">
            <div style="border: 1px solid #ccc">
              <Toolbar
                style="border-bottom: 1px solid #ccc"
                :editor="editor"
                :defaultConfig="toolbarConfig"
                :mode="mode"
              />
              <Editor
                style="height: 200px; overflow-y: hidden"
                v-model="html"
                :defaultConfig="editorConfig"
                :mode="mode"
                @onCreated="onCreated"
              />
            </div>
          </el-tab-pane>
        </el-tabs>
      </el-form>
      <el-divider />
      <el-button type="primary" @click="submitForm('form')">添加热映电影</el-button>
    </div>
  </div>
</template>

<script>
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
export default {
  components: { Editor, Toolbar },
  data() {
    return {
      video: '',
      form: {
        title: ''
      },
      activeName: '2',
      // 富文本编辑对象
      editor: null,
      // 富文本中的数据，初始化数怕大
      html: '',
      // 工具栏,可以自定义
      toolbarConfig: {},
      // 富文本设置 预设
      editorConfig: {
        placeholder: '请输入电影内容信息...',
        MENU_CONF: {
          color: {
            // colors: ['#000', '#333', '#666']
          }
        }
      },
      //  模式
      mode: 'default' // or 'simple'
      // mode: 'simple' // or 'simple'
    }
  },
  beforeDestroy() {
    this.editor && this.editor.destroy() // 组件销毁时，及时销毁编辑器
  },
  methods: {
    uploadSuccess(res) {
      if(res.code===0){
        this.video = res.data.path
      }
    },
    onCreated(editor) {
      // 一定要用 Object.seal() ，否则会报错
      this.editor = Object.seal(editor)
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log({ ...this.form, html: this.html })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style src="@wangeditor/editor/dist/css/style.css"></style>
<style lang="scss" scoped>
video{
  width: 480px;
  height: 272px;
}
// 定义变量
.container {
  .nav {
    display: flex;
    justify-content: space-between;
    padding-bottom: 10px;
    margin-bottom: 10px;
    border-bottom: 1px solid #ccc;
  }
}
</style>
